//
//  SJMessageCell.m
//  WSL
//
//  Created by CJ on 2018/2/3.
//  Copyright © 2018年 sanjuetech. All rights reserved.
//

#import "SJMessageCell.h"
#import "SJMessageItem.h"


@interface SJMessageCell ()

@property (strong, nonatomic) UIView *bgView;
@property (strong, nonatomic) UILabel *timeLabel;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UILabel *messageLabel;

@end

@implementation SJMessageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    self.backgroundColor = [UIColor clearColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [self.contentView addSubview:self.bgView];
    self.bgView.sd_layout
    .leftSpaceToView(self.contentView, SJMargin)
    .rightSpaceToView(self.contentView, SJMargin)
    .bottomSpaceToView(self.contentView, 0)
    .heightIs(SJItemWH * 2 + SJMargin);
    
    [self.bgView addSubview:self.titleLabel];
    self.titleLabel.sd_layout
    .leftSpaceToView(self.bgView, SJLayoutFrame(14))
    .topSpaceToView(self.bgView, SJLayoutFrame(23))
    .heightIs(SJLayoutFrame(15));
    [self.titleLabel setSingleLineAutoResizeWithMaxWidth:SJScreenW / 2];
    
    [self.bgView addSubview:self.messageLabel];
    self.messageLabel.sd_layout
    .leftSpaceToView(self.bgView, SJLayoutFrame(14))
    .topSpaceToView(self.titleLabel, SJLayoutFrame(13))
    .rightSpaceToView(self.bgView, SJLayoutFrame(14))
    .heightIs(SJLayoutFrame(12));
    
    [self.bgView addSubview:self.timeLabel];
    self.timeLabel.sd_layout
    .rightSpaceToView(self.bgView, SJLayoutFrame(10))
    .topSpaceToView(self.bgView, SJLayoutFrame(10))
    .heightIs(SJLayoutFrame(12));
    [self.timeLabel setSingleLineAutoResizeWithMaxWidth:SJScreenW / 2];
    
}

- (void)setItem:(SJMessageItem *)item {
    
    _item = item;
    
    _titleLabel.text = item.title?item.title:@"思藏官方";
    _messageLabel.text = item.message;
    _timeLabel.text = [self formatTimestamp2DateString:item.tm format:nil];
    
}

#pragma mark - getter

- (UIView *)bgView {
    
    if (!_bgView) {
        
        _bgView = [UIView new];
        _bgView.sd_cornerRadius = @(SJCornerRadius);
        _bgView.backgroundColor = SJColorHexString(@"#ffffff25");
    }
    return _bgView;
}

- (UILabel *)messageLabel {
    
    if (!_messageLabel) {
        
        _messageLabel = [UILabel new];
        _messageLabel.textColor = SJColorHexString(@"#ffffff");
        _messageLabel.font = [UIFont systemFontOfSize:SJLayoutFrame(12)];
        _messageLabel.textAlignment = NSTextAlignmentLeft;
        _messageLabel.numberOfLines = 1;
    }
    return _messageLabel;
}


- (UILabel *)titleLabel {
    
    if (!_titleLabel) {
        
        _titleLabel = [UILabel new];
        _titleLabel.textColor = SJColorHexString(@"#ffffff");
        _titleLabel.font = [UIFont systemFontOfSize:SJLayoutFrame(15)];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _titleLabel;
}

- (UILabel *)timeLabel {
    
    if (!_timeLabel) {
        
        _timeLabel = [UILabel new];
        _timeLabel.textColor = SJColorHexString(@"#ffffff");
        _timeLabel.font = [UIFont systemFontOfSize:SJLayoutFrame(12)];
        _timeLabel.textAlignment = NSTextAlignmentRight;
        _timeLabel.alpha = .6;
        
    }
    return _timeLabel;
}

#pragma mark - 时间戳→时间
- (NSString *)formatTimestamp2DateString:(NSString *)timestampString format:(NSString *)formatString {
    if (!timestampString || timestampString.length <= 0) {
        return nil;
    }
    NSTimeInterval timestamp = timestampString.length >= 13 ? timestampString.doubleValue / 1000.0 : timestampString.doubleValue;
    NSDate *detaildate = [NSDate dateWithTimeIntervalSince1970:timestamp];
    // 实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    // 设定时间格式
    [dateFormatter setDateFormat: formatString ?: @"yyyy-MM-dd"];
    return [dateFormatter stringFromDate: detaildate];
}

@end
